﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;

namespace EBA
{
    /// <summary>
    /// 运行环境信息。
    /// </summary>
    public static class AppRuntime
    {
        /// <summary>
        /// 是不是Web程序。
        /// </summary>
        public static bool IsWebApplication
        {
            get
            {
                return AppDomain.CurrentDomain.SetupInformation.ConfigurationFile.EndsWith("web.config",StringComparison.InvariantCulture);
            }
        }

        /// <summary>
        /// 程序所在根路径。
        /// </summary>
        public static string ApplicationPath
        {
            get
            {
                return AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            }
        }

        /// <summary>
        /// 程序运行文件路径。
        /// </summary>
        public static string AssemblyPath
        {
            get
            {
                if (IsWebApplication)
                {
                    return AppDomain.CurrentDomain.SetupInformation.ApplicationBase +"\\bin";
                }
                else
                {
                    return AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
                }
            }
        }

        /// <summary>
        /// 打开指定路径的配置信息。
        /// </summary>
        public static Configuration OpenConfiguration(string configPath=null)
        {
            var map = new ExeConfigurationFileMap { ExeConfigFilename = configPath??AppDomain.CurrentDomain.SetupInformation.ConfigurationFile };
            
            return ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
        }
    }
}
